package com.powernode.controller;

import com.powernode.constant.Constants;
import com.powernode.entity.Goods;
import com.powernode.entity.Orders;
import com.powernode.entity.Users;
import com.powernode.service.GoodsService;
import com.powernode.service.OrderService;
import com.powernode.vo.HttpResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Tag(name = "商品接口", description = "查询商品列表，商品明细，查看秒杀结果等信息")
@Controller
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private OrderService orderService;

    @GetMapping("/goods/list")
    public ModelAndView queryGoodsList(){
        List<Goods> goodsList = goodsService.queryGoodsList();
        ModelAndView mv = new ModelAndView("list");
        mv.addObject("goodsList", goodsList);
        return mv;
    }

    @GetMapping("/goods/{id}")
    public ModelAndView queryGoodsById(@PathVariable Integer id){
        Goods goods = goodsService.getById(id);
        ModelAndView mv = new ModelAndView("detail");
        mv.addObject("goods", goods);
        mv.addObject("nowTime", System.currentTimeMillis());
        return mv;
    }


    /**
     * 秒杀开始后获取随机码
     * 秒杀进行中才可以获取结果
     */
    @GetMapping("/goods/time/{id}")
    @ResponseBody
    public HttpResult goodsTime(@PathVariable Integer id, HttpServletRequest request) {
        //模拟登录
        Users user = Users.builder().id(1001).name("jack").build();

        //将用户对象放入session
        request.getSession().setAttribute(Constants.LOGIN_USER, user);

        //根据id获取商品信息
        Goods goods = goodsService.getById(id);

        //判断秒杀是否进行中

        long currentTime = System.currentTimeMillis();

        //获取秒杀开始和结束的时间
        long startTime = goods.getStartTime().getTime();
        long endTime = goods.getEndTime().getTime();

        if (currentTime < startTime) {
            return HttpResult.builder().code(Constants.ERROR).msg("秒杀未开始").build();
        }

        if (currentTime > endTime) {
            return HttpResult.builder().code(Constants.ERROR).msg("秒杀已结束").build();
        }

        //秒杀进行中
        return HttpResult.builder().code(Constants.SUCCESS).msg("秒杀进行中").data(goods.getRandomName()).build();
    }


    /**
     * 查询秒杀结果
     */

    @Operation(summary = "查询秒杀结果")
    @GetMapping("/goods/result/{id}")
    @ResponseBody
    public HttpResult queryResult(@PathVariable("id") Integer goodsId,HttpServletRequest request) {
        //获取用户信息
        Users user = (Users)request.getSession().getAttribute(Constants.LOGIN_USER);

        //查询订单
        Orders orders = orderService.queryResult(user.getId(), goodsId);

        if (orders != null) {
            return HttpResult.builder().code(Constants.SUCCESS).data("秒杀成功").build();
        }

        return HttpResult.builder().code(Constants.ERROR).msg("秒杀进行中").build();

    }




}
